package thread.threaddemo;

import java.time.LocalDateTime;

/**
 * @Author: wenjingyuan
 * @Date: 2022/11/17/20:26
 * @Description:内存可见性问题
 */
public class ThreadDemo17 {

    private static volatile boolean flag=true;

    public static void main(String[] args) {
        Thread t1=new Thread(()->{
            System.out.println("线程1开始执行"+ LocalDateTime.now());
            while(flag){
            }
            System.out.println("线程1结束执行"+LocalDateTime.now());
        });
        t1.start();
       Thread t2=new Thread(()->{
           try {
               Thread.sleep(1000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           System.out.println("修改flag"+LocalDateTime.now());
           flag=false;
       });
       t2.start();
    }
}
